<?php
use cassandra\IndexOperator;

use cassandra\IndexClause;

use cassandra\IndexExpression;

use phpcassa\Schema\DataType\UTF8Type;

use phpcassa\UUID;

use phpcassa\Util\Clock;

use phpcassa\ColumnSlice;

error_reporting ( E_ALL | E_STRICT );
date_default_timezone_set ( 'Asia/Bangkok' );
set_time_limit ( 0 );


set_include_path ( '.' . PATH_SEPARATOR . '../../library/' . PATH_SEPARATOR . '../../application/models' . PATH_SEPARATOR . get_include_path () );

require_once 'Zend/Config/Ini.php';
$config = new Zend_Config_Ini ( '../../application/configs/config.ini', 'development' );

require_once ('autoload.php');
use phpcassa\CassandraUtil;
use phpcassa\Connection\ConnectionPool;
use phpcassa\ColumnFamily;
use phpcassa\SystemManager;
use phpcassa\Schema\StrategyClass;

$sys = new SystemManager ( $config->db->host );

try {
	//create keyspace database
 	$sys->create_keyspace ( $config->db->name , array (
 			"strategy_class" => StrategyClass::SIMPLE_STRATEGY,
 			"strategy_options" => array (
 					'replication_factor' => '1' 
 			) 
 	));
} catch ( Exception $e ) {
 	echo "Keyspace exist \n";
} 	
 	
try {
	
	//create column family: Keyword
	$sys->create_column_family($config->db->name, 'Keyword', array(
	 			"column_type" => "Standard",
	 			"comparator_type" => "UTF8Type",
	 			"key_validation_class" => "UTF8Type",
	 			"default_validation_class" => "UTF8Type"
	));
	
	//create column family: Website
	$sys->create_column_family($config->db->name, 'Website', array(
			"column_type" => "Standard",
			"comparator_type" => "UTF8Type",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	//create column family: Crawler
	$sys->create_column_family($config->db->name, 'Crawler', array(
			"column_type" => "Standard",
			"comparator_type" => "UTF8Type",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	//create column family: CrawlerWebsiteUrl
	$sys->create_column_family($config->db->name, 'CrawlerWebsiteUrl', array(
			"column_type" => "Standard",
			"comparator_type" => "UTF8Type",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	$sys->create_index($config->db->name, 'CrawlerWebsiteUrl', 'crawler_id', "UTF8Type" );
	
	
	
	//create column family: Page
	$sys->create_column_family($config->db->name, 'Page', array(
			"column_type" => "Standard",
			"comparator_type" => "UTF8Type",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	$sys->create_index($config->db->name, 'Page', 'website_url', "UTF8Type" );
	
	
	//create column family: Data
	$sys->create_column_family($config->db->name, 'Data', array(
			"column_type" => "Standard",
			"comparator_type" => "UTF8Type",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	$sys->create_index($config->db->name, 'Data', 'crawler_id', "UTF8Type" );
	$sys->create_index($config->db->name, 'Data', 'website_url', "UTF8Type" );
	$sys->create_index($config->db->name, 'Data', 'page_url', "UTF8Type" );
	$sys->create_index($config->db->name, 'Data', 'keyword_name', "UTF8Type" );
	
	
	//create column family: History
	$sys->create_column_family($config->db->name, 'History', array(
			"column_type" => "Standard",
			"comparator_type" => "UTF8Type",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	//create column family: KeywordRanking
	$sys->create_column_family($config->db->name, 'KeywordRanking', array(
			"column_type" => "Standard",
			"comparator_type" => "UTF8Type",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	//create column family: WebsiteRanking
	$sys->create_column_family($config->db->name, 'WebsiteRanking', array(
			"column_type" => "Standard",
			"comparator_type" => "UTF8Type",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	//create column family: PageRanking
	$sys->create_column_family($config->db->name, 'PageRanking', array(
			"column_type" => "Standard",
			"comparator_type" => "UTF8Type",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	//create column family: CrawledWebsiteUrlList
	$sys->create_column_family($config->db->name, 'CrawledWebsiteUrlList', array(
			"column_type" => "Standard",
			"comparator_type" => "LongType",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	//create column family: KeywordRankinglList
	$sys->create_column_family($config->db->name, 'KeywordRankinglList', array(
			"column_type" => "Standard",
			"comparator_type" => "LongType",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	//create column family: WebsiteRankinglList
	$sys->create_column_family($config->db->name, 'WebsiteRankinglList', array(
			"column_type" => "Standard",
			"comparator_type" => "LongType",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	//create column family: PageRankinglList
	$sys->create_column_family($config->db->name, 'PageRankinglList', array(
			"column_type" => "Standard",
			"comparator_type" => "LongType",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	//create counts
	$sys->create_column_family($config->db->name, 'Count', array(
			"column_type" => "Standard",
			"comparator_type" => "UTF8Type",
			"key_validation_class" => "UTF8Type",
			"default_validation_class" => "UTF8Type"
	));
	
	

} catch ( Exception $e ) {
	echo "Create colunm family error please check  \n";
	echo $e->getMessage();
}

$sys->close();

return;
//	$sys->create_index($config->db->name, 'Keyword', 'name', "UTF8Type" );
	// Start a connection pool, create our ColumnFamily instance
	$pool = new ConnectionPool($config->db->name, array($config->db->host));
	$keywords = new ColumnFamily($pool, 'Keyword');
	//$keywords->set_autopack_keys(false);
	// Insert a few records
 	$keywords->insert('kinh doanh', array("name" => serialize(array(1=>"kinh doanh", 2=>"parent_name" ))  ) );
// 	$keywords->insert('dai gia', array("name" => "kinh doanh"));
// 	$keywords->insert('swz', array("name" => "kinh doanh"));
// 	$keywords->insert('sex', array("name" => "kinh doanh"));
// 	$keywords->insert('city', array("name" => "kinh doanh"));
	//$keywords->insert('key', array(1 => "kinh doanh", 2=> "b", 3 => "c", 4=> "d"));
	$start = microtime();
 	$kw1 = $keywords->get('kinh doanh');
 	$name_arr = unserialize($kw1['name']);
 	var_dump($name_arr);
 	$finish = microtime();
 	echo $finish-$start;
 	
	//batch insert
// 	for ($i=1;$i<50;$i++){
// 		$keywords->insert($i, array('name' => "kinh doanh-".$i));
// 	}
	
	//$rows = $keywords->get_range('key-1','key-2',20);
	//var_dump($rows);
	//echo "kw1:".$kw1['name']. "\n";
	
	//echo $keywords->pack_key('key-1');
	echo CassandraUtil::get_time();
	//drop keyspace
	//$sys->drop_keyspace($config->db->name);
// 	$arr_index_expression[] = CassandraUtil::create_index_expression('name', 'kinh doanh-28', IndexOperator::EQ);
// 	$index_clause = CassandraUtil::create_index_clause($arr_index_expression,'',10);
	
// 	$rows = $keywords->get_indexed_slices($index_clause);
	//$index_exp = new IndexExpression('name', 'kinh doanh-27');
 	//$index_clause = new IndexClause(array($index_exp));
 	//$rows = $keywords->get_indexed_slices($index_clause);
//  	foreach ($rows as $k){
//  		var_dump($k);
//  	}
	//remove all rows of keywords
	//$keywords->truncate();
	// Close our connections
//	$pool->close();
	


